import JsonP from 'jsonp'
import axios from 'axios'
import Utils from './../utils/utils'
import { Modal } from 'antd'  

export default class Axios {
    static requestList(_this, url, params, isMock) {
        var data = {
            param: params,
            isMock
        }
        this.ajax({
            url: url,
            data: data
        }).then((res)=> {
           if(res && res.result) {
            let list = res.result.item_list.map((item, index) => {
                item.key = index
                return item
            })
            _this.setState({
                list,
                pagination: Utils.pagination(res, (current) => {
                    _this.params.page = current;
                    _this.requestList();
                })
            })
           } 
        })
    }

    static jsonp(options) {
        return new Promise((resolve, reject) => {
            JsonP(options.url, {
                param: 'callback'
            }, function (err, response) {
                if (response.status == 'success') {
                    resolve(response);
                } else {
                    reject(response.messsage);
                }
            })
        })
    }

    static ajax(options){
        
        //请求时处理 loading
        let loading;
        if (options.data && options.data.isShowLoading !== false){
            loading = document.getElementById('ajaxLoading');
            loading.style.display = 'block';
        }

        // 判断是否是 mock 接口的数据
        let baseApi = ''
        if (options.isMock) {
            baseApi = ' https://www.easy-mock.com/mock/5bdc5b99f3dd6975f8bc732b/immocapi';
        } else {
            //这里改为原生的地址就好，真正的 后端地址
            baseApi = ' https://www.easy-mock.com/mock/5bdc5b99f3dd6975f8bc732b/immocapi';
        }
        // let baseApi = ' https://www.easy-mock.com/mock/5bdc5b99f3dd6975f8bc732b/immocapi';
        // let baseApi = 'https://www.easy-mock.com/mock/5a7278e28d0c633b9c4adbd7/api'
        return new Promise((resolve,reject)=>{
            axios({
                url:options.url,
                method:'get',
                baseURL:baseApi,
                timeout:5000,
                params: (options.data && options.data.params) || ''  //需要的参数
            }).then((response)=>{
                if (options.data && options.data.isShowLoading !== false) {
                    loading = document.getElementById('ajaxLoading');
                    loading.style.display = 'none';
                }
                //请求成功
                if (response.status == '200'){
                    let res = response.data;
                    if (res.code == '0'){
                        resolve(res);
                    }else{
                        Modal.info({
                            title:"提示",
                            content:res.msg //axios 请求报错的信息
                        })
                    }
                }else{
                    reject(response.data);
                }
            })
        });
    }
}